﻿/*
 * Load dữ liệu từ các bảng
 * 1. Training_TrainingProgramDetail
 * 2. Training_Subject
 * 3. Catalogue_TrainingTerm
 * 4. Catalogue_TrainingCourse
 * 5. Catalogue_Department
 * 6. Catalogue_TrainingMajor
 * 7. Catalogue_Level
 * 8. Catalogue_TrainingSujectType
 * 9. Catalogue_TrainingType
 * Lưu dữ liệu vào bảng
 * 1. Training_TrainingPropramSchedule
 * Version: 0.0.0.1
 * Author: Ho Tan Dat
 * Date Modify: 20/06/2012
*/
using System;
using UniMana.Utils.Data;
using DevExpress.XtraBars;
using System.Data;
using System.Data.Linq;
using System.Linq;
using System.Windows.Forms;
using UniMana.Data;
using DevExpress.XtraGrid;
namespace UniMana.Win.Training
{
    public partial class FrmTrainingProgram_SubSchedule : DevExpress.XtraEditors.XtraForm
    {
        #region [Khai báo biến]

        DBMain m_dataCatalogue = new DBMain();
        DBMain m_dataTrainingProgram = new DBMain();
        
        #endregion

        int proKey = -1;
        #region [Khởi tạo]
        public FrmTrainingProgram_SubSchedule()
        {
            InitializeComponent();
            SysUtils.SetRuleControl(this.Controls, this.Name);
            LoadData();
        }

        public FrmTrainingProgram_SubSchedule(int proKey)
        {
            InitializeComponent();
            SysUtils.SetRuleControl(this.Controls, this.Name);
            LoadData();

            this.proKey = proKey;
            var qSchedule = Training_TrainingProgram_ScheduleData.GetByTrainingProramKey(m_dataTrainingProgram, this.proKey);
            DataTable dtSchedule = qSchedule.ToDataTable();
            dtSchedule.Columns.Add("Numbers");
            this.grcTrainingSchedule.DataSource = dtSchedule;
            var qDetail = Traning_TrainingProgram_DetailData.GetDetailNotSchedule(m_dataTrainingProgram, proKey);
            this.grcTrainingDetail.DataSource = qDetail.ToDataTable();

            this.grvTrainingDetail.ExpandAllGroups();
            this.grvTrainingSchedule.ExpandAllGroups();
        }
        #endregion

        #region [Load dữ liệu]
        private void LoadData()
        {
            this.lueCatalogue_Department.GetData(Catalogue_DepartmentData.GetDepartmentStudentManage());

            this.lueCatalogue_Level.GetData(m_dataCatalogue.m_DCReadOnly.Catalogue_Levels);

            this.lueCatalogue_Major.GetData(Catalogue_MajorData.GetListByRef(this.lueCatalogue_Level.EditValue, this.lueCatalogue_Department.EditValue));

            this.lueCatalogue_TrainingType.GetData(m_dataCatalogue.m_DCReadOnly.Catalogue_TrainingTypes);
            this.lueCatalogue_TrainingType.EditValue = m_dataCatalogue.m_DCReadOnly.Catalogue_TrainingTypes.Where(p => p.Active == true).FirstOrDefault().Catalogue_TrainingTypeKey;

            this.lueCatalogue_Course.GetData(m_dataCatalogue.m_DCReadOnly.Catalogue_Courses);
            this.lueCatalogue_Course.Properties.Columns[0].SortOrder = DevExpress.Data.ColumnSortOrder.Descending;
            this.lueCatalogue_Course.EditValue = m_dataCatalogue.m_DCReadOnly.Catalogue_Courses.Where(p => p.Active == true).FirstOrDefault().Catalogue_CourseKey;

            this.lueTraining_TrainingProgram.GetData(Training_TrainingProgramData.GetListByRef(this.lueCatalogue_Level.EditValue,
                this.lueCatalogue_TrainingType.EditValue,
                this.lueCatalogue_Course.EditValue,
                this.lueCatalogue_Major.EditValue));

            this.lueCatalogue_Term.GetData(m_dataCatalogue.m_DCReadOnly.Catalogue_Terms);
            rluCatalogue_Term.GetData(m_dataCatalogue.m_DCReadOnly.Catalogue_Terms);      
        }

        #endregion

        #region [Sự kiện button]
        private void btnSave_Click(object sender, ItemClickEventArgs e)
        {
            
            DataTable mdtSchedule = grcTrainingSchedule.DataSource as DataTable;
         
            foreach (DataRow nRow in mdtSchedule.GetChanges().Rows)
            {

                Training_TrainingProgram_ScheduleInfo T_ScheduleInfo = new Training_TrainingProgram_ScheduleInfo(int.Parse(nRow["Training_TrainingProgramKey"].ToString()), int.Parse(nRow["Catalogue_TermKey"].ToString()), int.Parse(nRow["Training_SubjectKey"].ToString()));
                this.Validate();
                T_ScheduleInfo.Save();
            }
        }
        private void btnExport_Click(object sender, ItemClickEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter="*.xlsx | *.xls";
            sfd.Title = "Lưu tập tin excel 2007";
            sfd.ShowDialog();
            if(sfd.FileName!="")
            {
                this.grvTrainingSchedule.ExportToXlsx(sfd.FileName);
            }
        }
        #endregion

        private void lueCatalogue_Department_EditValueChanged(object sender, EventArgs e)
        {
            this.lueCatalogue_Major.GetData(Catalogue_MajorData.GetListByRef(this.lueCatalogue_Level.EditValue,this.lueCatalogue_Department.EditValue));

            this.lueTraining_TrainingProgram.GetData(Training_TrainingProgramData.GetListByRef(this.lueCatalogue_Level.EditValue,
                    this.lueCatalogue_TrainingType.EditValue,
                    this.lueCatalogue_Course.EditValue,
                    this.lueCatalogue_Major.EditValue));
        }

        private void lueCatalogue_Level_EditValueChanged(object sender, EventArgs e)
        {

            this.lueCatalogue_Major.GetData(Catalogue_MajorData.GetListByRef(this.lueCatalogue_Level.EditValue, this.lueCatalogue_Department.EditValue));

            this.lueTraining_TrainingProgram.GetData(Training_TrainingProgramData.GetListByRef(this.lueCatalogue_Level.EditValue,
                this.lueCatalogue_TrainingType.EditValue,
                this.lueCatalogue_Course.EditValue,
                this.lueCatalogue_Major.EditValue));

        }

        private void lueTraining_TrainingProgram_EditValueChanged(object sender, EventArgs e)
        {
            /*  Hữu bỏ ngày 20-11-2012 */

            /*var qSchedule = Training_TrainingProgram_ScheduleData.GetByTrainingProramKey(m_dataTrainingProgram, int.Parse(lueTraining_TrainingProgram.EditValue.ToString()));
            DataTable dtSchedule = qSchedule.ToDataTable();
            dtSchedule.Columns.Add("Numbers");
            this.grcTrainingSchedule.DataSource = dtSchedule;
            var qDetail = Traning_TrainingProgram_DetailData.GetDetailNotSchedule(m_dataTrainingProgram, int.Parse(lueTraining_TrainingProgram.EditValue.ToString()));
            this.grcTrainingDetail.DataSource = qDetail.ToDataTable();

            this.grvTrainingDetail.ExpandAllGroups();
            this.grvTrainingSchedule.ExpandAllGroups();*/
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
           try
           { 
               if (lueCatalogue_Term.EditValue != null)
              {
                
                int intMaxTotalsCredits = 40;
                int intTerm = int.Parse(lueCatalogue_Term.EditValue.ToString());
                int intTotalsCredit = Convert.ToInt16(grvTrainingSchedule.GetGroupSummaryValue(intTerm, (GridGroupSummaryItem)grvTrainingSchedule.GroupSummary[0]));
                foreach (int i in grvTrainingDetail.GetSelectedRows())
                {
                    intTotalsCredit = intTotalsCredit + Convert.ToInt16(grvTrainingDetail.GetRowCellValue(i, "TotalCredits"));
                }
               
                if (intTotalsCredit > intMaxTotalsCredits)
                {
                    MessageBox.Show("Số tín chỉ/học kì không vượt quá:" + intMaxTotalsCredits);
                    return;
                }

            
                DataTable ndtSchedule = this.grcTrainingSchedule.DataSource as DataTable;
               
                if (ndtSchedule.Rows.Count == 0)
                {
                    DataTable ndtDetail = this.grcTrainingDetail.DataSource as DataTable;
                    ndtSchedule = ndtDetail.Clone();
                    ndtSchedule.Columns.Add("Catalogue_TermKey");
                    
                    this.grcTrainingSchedule.DataSource = ndtSchedule;
                }
                if (ndtSchedule.Columns.Contains("Numbers") == false)
                {
                    ndtSchedule.Columns.Add("Numbers");
                }

                
                    foreach (int i in grvTrainingDetail.GetSelectedRows())
                    {
                        DataRow nRow = ndtSchedule.NewRow();
                        nRow["Training_SubjectKey"] = grvTrainingDetail.GetRowCellValue(i, "Training_SubjectKey");
                        /*nRow["Training_TrainingProgramKey"] = lueTraining_TrainingProgram.EditValue.ToString();*/
                        nRow["Training_TrainingProgramKey"] = this.proKey;

                        nRow["Catalogue_TermKey"] = lueCatalogue_Term.EditValue.ToString();

                        nRow["SubjectGroup"] = grvTrainingDetail.GetRowCellValue(i, "SubjectGroup");
                        nRow["SubjectID"] = grvTrainingDetail.GetRowCellValue(i, "SubjectID");
                        nRow["SubjectName"] = grvTrainingDetail.GetRowCellValue(i, "SubjectName");
                        nRow["TotalCredits"] = grvTrainingDetail.GetRowCellValue(i, "TotalCredits");
                        nRow["SubjectType"] = grvTrainingDetail.GetRowCellValue(i, "SubjectType");

                        ndtSchedule.Rows.Add(nRow);
                    }
                    grvTrainingDetail.DeleteSelectedRows();
                }
                else
                {
                    MessageBox.Show("Vui lòng chọn học kì trước");
                }
            }
            catch { }
            
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable ndtDetail = this.grcTrainingDetail.DataSource as DataTable;

                foreach (int i in grvTrainingSchedule.GetSelectedRows())
                {
                    DataRow nRow = ndtDetail.NewRow();

                    /*nRow["Training_TrainingProgramKey"] = lueTraining_TrainingProgram.EditValue.ToString();*/
                    nRow["Training_TrainingProgramKey"] = this.proKey;
                    nRow["Training_SubjectKey"] = this.grvTrainingSchedule.GetRowCellValue(i, "Training_SubjectKey");
                    nRow["SubjectType"] = grvTrainingSchedule.GetRowCellValue(i, "SubjectType");

                    nRow["SubjectGroup"] = grvTrainingSchedule.GetRowCellValue(i, "SubjectGroup");

                    nRow["SubjectID"] = grvTrainingSchedule.GetRowCellValue(i, "SubjectID");
                    nRow["SubjectName"] = grvTrainingSchedule.GetRowCellValue(i, "SubjectName");
                    nRow["TotalCredits"] = grvTrainingSchedule.GetRowCellValue(i, "TotalCredits");

                    ndtDetail.Rows.Add(nRow);
                    Training_TrainingProgram_ScheduleInfo T_ScheduleInfo = new Training_TrainingProgram_ScheduleInfo(int.Parse(nRow["Training_TrainingProgramKey"].ToString()), int.Parse(grvTrainingSchedule.GetRowCellValue(i, "Catalogue_TermKey").ToString()), int.Parse(nRow["Training_SubjectKey"].ToString()));
                    T_ScheduleInfo.Delete();
                    T_ScheduleInfo.Save();
                }
               
                grvTrainingSchedule.DeleteSelectedRows();

                //grcTrainingSchedule.RefreshDataSource();

                //DataTable ndtSchedule = this.grcTrainingSchedule.DataSource as DataTable;
                
              
                //this.Validate();
                //int n = grvTrainingSchedule.RowCount;
            }
            catch { };
        
        }
  
        private void grvTrainingSchedule_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.Column == colNumbers && e.IsGetData)
            {
                 e.Value = e.Row;
            } 
        }

        private void lueCatalogue_Term_EditValueChanged(object sender, EventArgs e)
        {
            grvTrainingSchedule.ExpandGroupRow(-int.Parse(lueCatalogue_Term.EditValue.ToString()),true);
        }

        private void lueCatalogue_TrainingType_EditValueChanged(object sender, EventArgs e)
        {
            this.lueTraining_TrainingProgram.GetData(Training_TrainingProgramData.GetListByRef(this.lueCatalogue_Level.EditValue,
                this.lueCatalogue_TrainingType.EditValue,
                this.lueCatalogue_Course.EditValue,
                this.lueCatalogue_Major.EditValue));
        }

        private void lueCatalogue_Course_EditValueChanged(object sender, EventArgs e)
        {
            this.lueTraining_TrainingProgram.GetData(Training_TrainingProgramData.GetListByRef(this.lueCatalogue_Level.EditValue,
                this.lueCatalogue_TrainingType.EditValue,
                this.lueCatalogue_Course.EditValue,
                this.lueCatalogue_Major.EditValue));
        }

        private void lueCatalogue_Major_EditValueChanged(object sender, EventArgs e)
        {
            this.lueTraining_TrainingProgram.GetData(Training_TrainingProgramData.GetListByRef(this.lueCatalogue_Level.EditValue,
               this.lueCatalogue_TrainingType.EditValue,
               this.lueCatalogue_Course.EditValue,
               this.lueCatalogue_Major.EditValue));
        }

        private void ckeNotSchedule_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                var qDetail = Traning_TrainingProgram_DetailData.GetDetailNotSchedule(m_dataTrainingProgram, int.Parse(lueTraining_TrainingProgram.EditValue.ToString()));
                this.grcTrainingDetail.DataSource = qDetail.ToDataTable();
                this.grvTrainingDetail.ExpandAllGroups();
                this.grvTrainingSchedule.ExpandAllGroups();
                btnAdd.Visible = true;
            }
            catch { }
        }

        private void ckeScheduled_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                var qDetail = Traning_TrainingProgram_DetailData.GetDetailInSchedule(m_dataTrainingProgram, int.Parse(lueTraining_TrainingProgram.EditValue.ToString()));
                this.grcTrainingDetail.DataSource = qDetail.ToDataTable();
                this.grvTrainingDetail.ExpandAllGroups();
                this.grvTrainingSchedule.ExpandAllGroups();
                btnAdd.Visible = false;
            }
            catch { }
        }

        private void ckeAll_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                var qDetail = Traning_TrainingProgram_DetailData.GetListDetailTrainingProramKey(m_dataTrainingProgram, int.Parse(lueTraining_TrainingProgram.EditValue.ToString()));
                this.grcTrainingDetail.DataSource = qDetail.ToDataTable();
                this.grvTrainingDetail.ExpandAllGroups();
                this.grvTrainingSchedule.ExpandAllGroups();
                btnAdd.Visible = false;
            }
            catch { }
        }

        private void grvTrainingSchedule_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            //rowIndex = e.RowHandle;
            //if (rowIndex >= 0)
            //{
            //    rowIndex++;
            //    grvTrainingSchedule.SetRowCellValue(e.RowHandle, colNumbers, rowIndex);
            //}
        }

        private void grvTrainingSchedule_RowCountChanged(object sender, EventArgs e)
        {
            DataTable dtSchedule = grcTrainingSchedule.DataSource as DataTable;
            try
            {
                if (dtSchedule != null)
                {
                    for (int i = 1; i <= 9; i++)
                    {
                        int number = 1;
                        foreach (DataRow nRow in dtSchedule.Rows)
                        {
                            if (int.Parse(nRow["Catalogue_TermKey"].ToString()) == i)
                            {

                                nRow["Numbers"] = number;
                                number++;
                            }
                        }
                    }

                    int numRequired = 0;
                    int numCustom = 0;
                    foreach (DataRow dr in dtSchedule.Rows)
                    {
                        if (dr["SubjectType"].ToString() == "Môn bắt buộc")
                        {
                            numRequired = numRequired + int.Parse(dr["TotalCredits"].ToString());
                        }
                        if (dr["SubjectType"].ToString() == "Môn tự chọn")
                        {
                            numCustom = numCustom + int.Parse(dr["TotalCredits"].ToString());
                        }
                    }
                    lblSum.Text = "Số tín chỉ bắt buộc: " + numRequired + " tự chọn: " + numCustom;
                }
            }
            catch { }
        }

       
    }
}